//
//  THENetwork.swift
//  bridging_js
//
//  Created by cyptt on 2021/9/27.
//

import UIKit
import Alamofire
public class CHNetManage: NSObject {

    
    /*
     下载文件
     @param url 路径
     @param method 默认get
     @param param 参数
     @param headerDic请求头
     @param queue 队列
     @param saveFileName  保存的文件名  如果传入文件名相同，者会覆盖原来的文件
     */
    public class func download(url:String,method: HTTPMethod = .get,param:[String:Any]?,headerDic:[String:Any]?,queue:DispatchQueue? = nil,saveFileName:String? = nil,success: @escaping SuccessBlock,failure:@escaping FailureStrB){
        
        print("method",method)
            
           var encoding:ParameterEncoding = URLEncoding.default;
    
           let contentType:String? = headerDic?["Content-Type"] as? String
           if  contentType != nil && contentType!.contains("application/json") {
               encoding =  JSONEncoding.default;
           }
           var headers2:HTTPHeaders = []
           if headerDic != nil{
               headerDic?.forEach { (arg0) in
                   let (key, value) = arg0
                   headers2.add(name: key, value: value as! String)
               }
           }
        
        CHAFNetRequest.download(url: url, param: param,method: method, headers: headers2, encoding: encoding, queue: queue, saveFileName: saveFileName,success: success,failure: failure)
          
    }
    //MARK: - GET请求
    /**
        get请求不需要传header  --可以废弃被需要传header取代
     */
    public class func GET(url:String,param:[String:Any]?,success: @escaping SuccessBlock,failure:@escaping FailureStrB) {
        if param != nil {
        
        }
   
        let headers:HTTPHeaders = [] ;
        let encoding:ParameterEncoding = URLEncoding.default;
     
    
        CHAFNetRequest.requestNet(url: url, param: param,method: .get, headers: headers, encoding: encoding, queue: nil,success: success,failure: failure)
    
    }
    
    /**
        post请求不需要传header  --可以废弃被需要传header取代
     */
    public class func POST(url:String,param:[String:Any]?,cTypeJson:Bool,success: @escaping SuccessBlock,failure:@escaping FailureStrB){
        
        var headers:HTTPHeaders = [] ;
        var encoding:ParameterEncoding = URLEncoding.default;
        if cTypeJson {
            headers = ["Content-Type":"application/json;charset=UTF-8"]
            encoding =  JSONEncoding.default;
        }
        CHAFNetRequest.requestNet(url: url, param: param,method: .post, headers: headers, encoding: encoding, queue: nil, success: success,failure: failure)
    }
    
    

    /**
      post请求 需要传header 需要queue
     */
    public class func POSTWithHeaders(url:String,param:[String:Any]?,headerDic:[String:Any]?,queue:DispatchQueue? = nil,success: @escaping SuccessBlock,failure:@escaping FailureStrB){
        
        var encoding:ParameterEncoding = URLEncoding.default;
        let contentType:String? = headerDic?["Content-Type"] as? String
        if  contentType != nil && contentType!.contains("application/json") {
            encoding =  JSONEncoding.default;
        }
       
        var headers2:HTTPHeaders = []
        if headerDic != nil{
            headerDic?.forEach { (arg0) in
                let (key, value) = arg0
                headers2.add(name: key, value: value as! String)
            }
        }
        CHAFNetRequest.requestNet(url: url, param: param,method: .post, headers: headers2, encoding: encoding, queue: queue, success: success,failure: failure)
    }
    
   
    /**
        get请求需要传header 需要queue
     */
    public class func GETWithHeaders(url:String,param:[String:Any]?,headerDic:[String:Any]?,queue:DispatchQueue? = nil,success: @escaping SuccessBlock,failure:@escaping FailureStrB) {
     
     
        var encoding:ParameterEncoding = URLEncoding.default;
        let contentType:String? = headerDic?["Content-Type"] as? String
        if  contentType != nil && contentType!.contains("application/json") {
            encoding =  JSONEncoding.default;
        }
       

        var headers2:HTTPHeaders = []
        if headerDic != nil{
            headerDic?.forEach { (arg0) in
                let (key, value) = arg0
                headers2.add(name: key, value: value as! String)
            }
        }
       
     
        CHAFNetRequest.requestNet(url: url, param: param,method: .get, headers: headers2, encoding: encoding, queue: queue,success: success,failure: failure)
    }
    
    
    
    /*
     上传文件
     */
    public class func uploadFile(url: String, imageData:Data?, parameters: [String : Any]?, headerDics:[String:Any]?,withName:String = "file",fileName:String? = nil,success: @escaping SuccessBlock, failure:@escaping FailureStrB){
        var httpHeaders:HTTPHeaders? = nil
        if headerDics != nil {
            httpHeaders = [:]
            headerDics?.forEach { (arg0) in
                let (key, value) = arg0
                httpHeaders!.add(name: key, value: value as! String)
            }
        }
        
        
        CHAFNetRequest.uploadImage(url: url, imageData: imageData, parameters: parameters, headers: httpHeaders,withName: withName,fileName: fileName,success: success, failure: failure)
    }
    
    /*
     上传多个图片
     */
    public class func uploadFiles(url: String, imageDatas:[Data]?, parameters: [String : Any]?, headerDics:[String:Any]?,withName:String = "file",fileName:String? = nil,success: @escaping SuccessBlock, failure:@escaping FailureStrB){
        var httpHeaders:HTTPHeaders? = nil
        if headerDics != nil {
            httpHeaders = [:]
            headerDics?.forEach { (arg0) in
                let (key, value) = arg0
                httpHeaders!.add(name: key, value: value as! String)
            }
        }
        
    
        CHAFNetRequest.uploadImages(url: url, imageDatas: imageDatas, parameters: parameters, headers: httpHeaders,withName: withName,fileName: fileName, success: success, failure: failure)
    }
    
    
    /*
     取消所有网络请求
     */
    public class func cancelAllNet(){
        CHAFNetRequest.cancelAllNet()
    }
}

